# @Time:2021/6/11 10:11
# @Author:5120195554王欣霏
# -*- coding = utf-8 -*-

import os
import pygame
import enum


# 渐变功能——枚举状态
class SceneStatus(enum.IntEnum):
    In = 1  # 进入状态
    Normal = 2  # 正常状态
    Out = 3  # 出去状态


# 渐变功能——场景
class FadeScene(object):
    def __init__(self, back_image: pygame.image):
        self.back_image = back_image
        self.alpha = 0
        self.status = SceneStatus.In  # 初始状态是In

    def set_status(self, status: SceneStatus):
        self.status = status
        if status == SceneStatus.In:
            self.alpha = 0  # 透明
        if status == SceneStatus.Normal:
            self.alpha = 255  # 不透明
        if status == SceneStatus.Out:
            self.alpha = 0  # 透明

    def get_back_image(self, win_pos_x: int, win_pos_y: int):
        # 越界判断
        if win_pos_x < 0:
            win_pos_x = 0
        if win_pos_y < 0:
            win_pos_y = 0
        if win_pos_x > self.back_image.get_width() - 800:
            win_pos_x = self.back_image.get_width() - 800
        if win_pos_y > self.back_image.get_height() - 600:
            win_pos_y = self.back_image.get_height() - 600

        temp_surface = self.back_image.subsurface((win_pos_x, win_pos_y, 800, 600))
        if self.status == SceneStatus.Normal:
            return temp_surface  # 如果当前状态是正常状态 直接把图层返回
        elif self.status == SceneStatus.In:
            temp_surface.set_alpha(self.alpha)
            black_surface = pygame.Surface((800, 600))  # 创建一个纯黑的图层
            black_surface.blit(temp_surface, (0, 0))
            self.alpha += 20
            if self.alpha >= 255:
                self.alpha = 0
                self.status = SceneStatus.Normal
            return black_surface
        elif self.status == SceneStatus.Out:
            temp_surface.set_alpha(255 - self.alpha)
            black_surface = pygame.Surface((800, 600))
            black_surface.blit(temp_surface, (0, 0))
            self.alpha += 20
            if self.alpha >= 255:
                self.alpha = 255
            return black_surface

    def get_out(self):
        if self.status == SceneStatus.Out and self.alpha == 255:  # 当状态是Out的时候（转变开始） && alpha值是255（转变结束）
            return True
        else:
            return False
